package net.vkits.admin.application;

import net.vkits.admin.domain.model.FakeUser;
import net.vkits.admin.domain.model.Post;
import net.vkits.admin.domain.model.SubPost;
import net.vkits.admin.domain.repository.FakeUserRepository;
import net.vkits.admin.domain.repository.PostRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * Created by ethan-liu on 2017/2/3.
 */
@Service
public class PostApplicationService {
    private static final Logger logger = LoggerFactory.getLogger(RateApplicationService.class);

    @Resource
    private PostRepository postRepository;

    @Resource
    private FakeUserRepository fakeUserRepository;

    public Page<Post> getPosts(Pageable pageable) {
        return postRepository.findAll(pageable);
    }

    public Page<Post> getPostsByVisibilityTrue(Pageable pageable) {
        return postRepository.findByVisibilityTrue(pageable);
    }

    public Page<Post> getPostsByCellphoneOrVisibilityTrue(String cellphone, Pageable pageable) {
        return postRepository.findByCellphoneOrVisibilityTrue(cellphone, pageable);
    }

    public List<Post> getPosts() {
        return postRepository.findAll();
    }

    @Transactional
    public void addPost(String nickname, String cellphone, String avatarUrl, String content, int readNum, boolean visibility, int likeNum) {
        Post post = new Post(nickname, cellphone, avatarUrl, content, readNum, visibility, likeNum);
        postRepository.save(post);
    }

    @Transactional
    public void delPost(Long id) {
        postRepository.delete(id);
    }

    public Post getPost(Long id) {
        return postRepository.findOne(id);
    }

    @Transactional
    public void updatePost(Long postId, int readNum, int likeNum, boolean visibility, String content) {
        Optional<Post> postOptional = postRepository.findById(postId);
        postOptional.ifPresent(post -> {
            if (readNum != 0) {
                post.setReadNum(readNum);
            }
            if (likeNum != 0) {
                post.setLikeNum(likeNum);
            }
            post.setVisibility(visibility);
            if (StringUtils.isNotBlank(content)) {
                post.setContent(content);
            }
            postRepository.save(post);
        });
    }

    @Transactional
    public void likePost(Long id) {
        Optional<Post> postOptional = postRepository.findById(id);
        postOptional.ifPresent(post -> {
            post.addLikeNum();
            postRepository.save(post);
        });
    }

    @Transactional
    public void addSubPost(SubPost subPost) {
        Optional<Post> postOptional = postRepository.findById(subPost.getId());
        FakeUser fakeUser = fakeUserRepository.findOne(Long.parseLong(subPost.getNickname()));
        postOptional.ifPresent(post -> {
            SubPost subPost1 = new SubPost();
            subPost1.setNickname(fakeUser.getNickname());
            subPost1.setCellphone("13811480360");
            subPost1.setAvatarUrl(fakeUser.getAvatarUrl());
            subPost1.setContent(subPost.getContent());
            Set<SubPost> subPosts = post.getSubPosts();
            subPosts.add(subPost1);
            post.plusFollowNum();
            post.setFollowDate();
            postRepository.save(post);
        });
    }

    @Transactional
    public void addRealSubPost(SubPost subPost) {
        Optional<Post> postOptional = postRepository.findById(subPost.getId());
        postOptional.ifPresent(post -> {
            SubPost subPost1 = new SubPost();
            subPost1.setNickname(subPost.getNickname());
            subPost1.setCellphone(subPost.getCellphone());
            subPost1.setAvatarUrl(subPost.getAvatarUrl());
            subPost1.setContent(subPost.getContent());
            Set<SubPost> subPosts = post.getSubPosts();
            subPosts.add(subPost1);
            post.plusFollowNum();
            post.setFollowDate();
            postRepository.save(post);
        });
    }

    @Transactional
    public void delSubPost(Long id, Long subId) {
        Optional<Post> postOptional = postRepository.findById(id);
        postOptional.ifPresent(post -> {
            Set<SubPost> subPosts = post.getSubPosts();
            subPosts.removeIf(subPost -> subPost.getId().equals(subId));
            post.minusFollowNum();
            postRepository.save(post);
        });
    }
}
